package eu.jpereira.trainings.designpatterns.creational.builder.xml;

import eu.jpereira.trainings.designpatterns.creational.builder.model.ReportBody;
import eu.jpereira.trainings.designpatterns.creational.builder.ReportBodyBuilder;

public class XMLReportBodyBuilder implements ReportBodyBuilder {

        private XMLReportBody reportBody;
        private boolean withItems = false;
       
        public XMLReportBodyBuilder() {
               
                reportBody = new XMLReportBody();
        }
       
        public ReportBodyBuilder setCustomerName(String customerName) {
               
                reportBody.putContent("<sale><customer><name>");
                reportBody.putContent(customerName);
                reportBody.putContent("</name>");
                return this;
        }

        public ReportBodyBuilder setCustomerPhone(String phoneNumber) {
               
                reportBody.putContent("<phone>");
                reportBody.putContent(phoneNumber);
                reportBody.putContent("</phone></customer>");
                return this;
        }

        public ReportBodyBuilder withItems() {
               
                if (!withItems) {
                        reportBody.putContent("<items>");
                        withItems = true;
                }
                else {
                        reportBody.putContent("</items>");
                        withItems = false;
                }
               
                return this;
        }

        public ReportBodyBuilder newItem(String name, int quantity, double price) {
               
                reportBody.putContent("<item><name>");
                reportBody.putContent(name);
                reportBody.putContent("</name><quantity>");
                reportBody.putContent(quantity);
                reportBody.putContent("</quantity><price>");
                reportBody.putContent(price);
                reportBody.putContent("</price></item>");
                return this;
        }
       
        public ReportBodyBuilder endReport() {
               
                reportBody.putContent("</sale>");
                return this;
        }

        public ReportBody getReportBody() {
               
                return reportBody;
        }
}
